/**   
* @Title: SpiderTaskJob.java
* @Package org.xaoyaoyao.spider.job
* @Description: TODO
* @author xaoyaoyao
* @date Jan 25, 2019 5:36:35 PM
* @version V1.0   
*/
package org.xaoyaoyao.spider.job;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.xaoyaoyao.spider.entity.SpiderTask;
import org.xaoyaoyao.spider.redis.RedisService;
import org.xaoyaoyao.spider.util.Constants;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: SpiderTaskJob
 * @Description: TODO
 * @author xaoyaoyao
 * @date Jan 25, 2019 5:36:35 PM
 * 
 */
@Slf4j
@Service
public class SpiderTaskJob {

	@Autowired
	private RedisService redisService;

	@Scheduled(cron = "0 0/30 * * * ?")
	public void execute() {
		log.info(">>>>>>>> SpiderTaskJob Start！！！");
		try {
			// TODO 处理任务
			List<SpiderTask> tasks = new ArrayList<>();
			long queueSize = redisService.llen(Constants.SPIDER_TASK_KEYS);
			if (CollectionUtils.isNotEmpty(tasks) && queueSize < Constants.MAX_QUEUE_SIZE) {
				tasks.stream().forEach(task -> {
					redisService.lpush(Constants.SPIDER_TASK_KEYS, task.toString());
				});
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		log.info(">>>>>>>> SpiderTaskJob End！！！");
	}
}
